package com.hevs.adibilis;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ActivitiesActivity extends Activity {

	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.activity_activities);

		
		// getting intent data
        Intent in = getIntent();
        
        // Get JSON values from previous intent
        String id = in.getStringExtra(JSONFactory.TAG_ID);
        
       //=======================
		this.listView = (ListView) findViewById(R.id.listViewActivities);
		
		
		if(Utils.hasInternet(this)){
			new DownloadDataFromWeb().execute(id);
		} else {
			Toast.makeText(this, R.string.PleaseConnectToInternet, Toast.LENGTH_LONG).show();
		}
        		
	}

	// Uses AsyncTask to create a task away from the main UI thread. This task takes a 
	// URL string and uses it to create an HttpUrlConnection. Once the connection
	// has been established, the AsyncTask downloads the contents of the webpage as
	// an InputStream. Finally, the InputStream is converted into a string, which is
	// displayed in the UI by the AsyncTask's onPostExecute method.
	private class DownloadDataFromWeb extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {


		// onPostExecute displays the results of the AsyncTask.
		protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

			/**
			 * Updating parsed JSON data into ListView
			 * */
			 
			ListAdapter adapter = new SimpleAdapter(getApplicationContext(), result,
					R.layout.list_item_activity,
					new String[] { JSONFactory.TAG_ID, JSONFactory.TAG_REFERENCE, JSONFactory.TAG_NAME }, new int[] {
				R.id.idActivity, R.id.referenceActivity, R.id.nameActivityTextView });

			listView.setAdapter(adapter);
			listView.setOnItemClickListener(new OnItemClickListener(){ //

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					// getting values from selected ListItem
					String id_activity = ((TextView) view.findViewById(R.id.idActivity)).getText().toString(); //TODO chercher dans le hash
					String name = ((TextView) view.findViewById(R.id.nameActivityTextView)).getText().toString();

					// Starting new intent
					Intent in = new Intent(getApplicationContext(), DetailActivityActivity.class);
					in.putExtra(JSONFactory.TAG_ID, id_activity);
					in.putExtra(JSONFactory.TAG_NAME, name);

					startActivity(in);

				}

			});

		}

		@Override
		protected ArrayList<HashMap<String, String>> doInBackground(String... idString) {

			try {
				JSONFactory.getActivitiesForCategory(idString[0]);
				return JSONFactory.parseActivitiesForCategory();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoNodesException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
			
		}
	}


}
